<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/20
 * Time: 9:40
 */
namespace app\admin\controller;
use think\Db;
class Shop extends Base {
    /***品牌**/
    public function brand(){
        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['p_name'] = ['like',"%" . $key . "%"];
            }
            $map['p_status'] = 1;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_pinpai')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_pinpai')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * 品牌添加
     */
    public function addbrand(){
        if(request()->isAjax()){
            $result['p_name'] = input('param.p_name');
            $aaa = Db::table('up_pinpai')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed
     *品牌编辑
     */
    public function updatebrand(){
        if(request()->isAjax()){
            $whereMap['p_id'] = input('param.id');
            $result['p_name'] = input('param.p_name');
            $aaa = Db::table('up_pinpai')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $where['p_id'] = $id;
        $data = Db::table('up_pinpai')->where($where)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function delbrand(){
        $id = input('param.id');
        $where['p_id'] = $id;
        $data['p_status'] = 2;
        $res = Db::table('up_pinpai')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * 型号
     */
    public function version(){
        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['x_name'] = ['like',"%" . $key . "%"];
            }
            $map['x_status'] = 1;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_xinghao')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_xinghao')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 型号添加
     */
    public function addversion(){
        if(request()->isAjax()){
            $result['x_name'] = input('param.x_name');
            $res = Db::table('up_xinghao')
                ->where('x_name', $result['x_name'])
                ->where('x_status', 1)
                ->find();
            if($res){
                return json(['code' => 100, 'msg' => '该型号已存在，请不要重复添加']);
            }
            $aaa = Db::table('up_xinghao')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 修改型号
     */
    public function updateversion(){
        if(request()->isAjax()){
            $whereMap['x_id'] = input('param.id');
            $result['x_name'] = input('param.x_name');
    
            $res = Db::table('up_xinghao')
                ->where('x_name', $result['x_name'])
                ->where('x_status', 1)
                ->find();
            if($res){
                return json(['code' => 100, 'msg' => '该型号已存在']);
            }
            
            $aaa = Db::table('up_xinghao')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $where['x_id'] = $id;
        $data = Db::table('up_xinghao')->where($where)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /****
     * 型号删除
     */
    public function delversion(){
        $id = input('param.id');
        $where['x_id'] = $id;
        $data['x_status'] = 2;
        $res = Db::table('up_xinghao')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    /****
     * @return mixed
     * 商品列表
     */
    public function index(){

        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['good_name'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.good_ss_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.good_ss_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.good_ss_time'] = ['between time',[$start,$end]];
            }
            $map['a.good_status'] =1;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_goods')->alias('a')
                ->join('up_pinpai up','up.p_id = a.good_pinpai','left')
                ->join('up_xinghao ux','ux.x_id = a.good_xinghao','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_goods')
                ->alias ('a')
                ->join('up_pinpai up','up.p_id = a.good_pinpai','left')
                ->join('up_xinghao ux','ux.x_id = a.good_xinghao','left')
                ->page($page, $limit)
                ->where($map)
                ->field(
                    'up.p_name,ux.x_name,
                    good_thumb,a.y_price,
                    a.good_id,a.good_name,a.good_price,a.good_huohao,a.good_guige,a.good_chang,
                    a.good_kuan,a.good_gao,a.good_mao,a.good_jing,a.good_create_time,a.good_ss_time,
                    a.good_top,a.good_hot'
                )
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 商品添加
     */
    public function add(){
        if(request()->isAjax()){
            $data['good_name'] = input('param.good_name');
            $data['good_price'] = input('param.good_price');
            $data['y_price'] = input('param.y_price');
            $data['good_huohao'] = input('param.good_huohao');
            $data['good_guige'] = input('param.good_guige');
            $data['good_chang'] = input('param.good_chang');
            $data['good_kuan'] = input('param.good_kuan');
            $data['good_gao'] = input('param.good_gao');
            $data['good_mao'] = input('param.good_mao');
            $data['good_jing'] = input('param.good_jing');
            $data['good_ss_time'] = input('param.start');
            $data['good_pinpai'] = input('param.good_pinpai');
            $data['good_xinghao'] = input('param.good_xinghao');
            $data['good_thumb'] = input('param.images');
            $data['good_image'] = input('param.photo');
            $data['good_content'] = input('param.good_content');
            $data['good_canshu'] = input('param.good_canshu');
            $data['good_hot'] = input('param.good_hot');
            $data['good_create_time'] = date('Y-m-d H:i:s',time());
            $aaa = Db::table('up_goods')->insertGetId($data);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $pinpai = Db::table('up_pinpai')->where('p_status',1)->select();
        $xinghao = Db::table('up_xinghao')->where('x_status',1)->select();
        $this->assign('pinpai',$pinpai);
        $this->assign('xinghao',$xinghao);
        return $this->fetch();
    }

    /***
     * @return mixed
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $where['good_id'] = input('param.id');
            $data['good_name'] = input('param.good_name');
            $data['good_price'] = input('param.good_price');
            $data['y_price'] = input('param.y_price');
            $data['good_huohao'] = input('param.good_huohao');
            $data['good_guige'] = input('param.good_guige');
            $data['good_chang'] = input('param.good_chang');
            $data['good_kuan'] = input('param.good_kuan');
            $data['good_gao'] = input('param.good_gao');
            $data['good_mao'] = input('param.good_mao');
            $data['good_jing'] = input('param.good_jing');
            $data['good_ss_time'] = input('param.start');
            $data['good_pinpai'] = input('param.good_pinpai');
            $data['good_xinghao'] = input('param.good_xinghao');

            $data['good_content'] = input('param.good_content');
            $data['good_canshu'] = input('param.good_canshu');
            if(input('param.images')!=''){
                $data['good_thumb'] = input('param.images');
            }
            if(input('param.photo')!=''){
                $data['good_image'] = input('param.photo');
            }
            $aa = Db::table('up_goods')->where($where)->update($data);

            if($aa){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $id = input('param.id');
        $where['good_id'] = $id;
        $res = Db::table('up_goods')->alias('a')
            ->where($where)
            ->find();
        $img = explode(',',$res['good_image']);
        if($img[0]!=''){
            foreach ($img as $key=>&$vo){
                $vo = $vo;
            }
            $res['good_image'] = implode(',',$img);
        }


        $pinpai = Db::table('up_pinpai')->where('p_status',1)->select();
        $xinghao = Db::table('up_xinghao')->where('x_status',1)->select();
        $this->assign('pinpai',$pinpai);
        $this->assign('xinghao',$xinghao);
        $this->assign('res',$res);
        return $this->fetch();
    }
    /****
     * @return \think\response\Json
     * 删除商品
     */
    public function del(){
        $id = input('param.id');
        $where['good_id'] = $id;
        $data['good_status'] =2;
        $res = Db::table('up_goods')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /****
     * 下架热门商品
     */
    public function delhot(){
        $id = input('param.id');
        $where['good_id'] = $id;
        $data['good_hot'] =1;
        $res = Db::table('up_goods')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }

    /*****
     * @return \think\response\Json
     * 上架热门商品
     */
    public function hot(){
        $id = input('param.id');
        $where['good_id'] = $id;
        $data['good_hot'] =2;
        $res = Db::table('up_goods')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }

    /***
     * 上架精品
     */
    public function top(){
        $id = input('param.id');
        $where['good_id'] = $id;
        $data['good_top'] =2;
        $whereMap['good_top'] = 2;
        $num = Db::table('up_goods')->where($whereMap)->count();
        if($num >= 3){
            return json(['code'=>100,'msg'=>'精品商品最多3个']);
        }
        $res = Db::table('up_goods')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }


    /****
     * 下架精品
     */
    public function deltop(){
        $id = input('param.id');
        $where['good_id'] = $id;
        $data['good_top'] =1;
        $res = Db::table('up_goods')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }
}